package com.example.lbs;

import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.content.Context;
import android.telephony.CellLocation;
import android.telephony.TelephonyManager;
import android.telephony.gsm.GsmCellLocation;
import android.util.Log;
import android.view.Menu;
import android.widget.TextView;

public class LbsActivity extends Activity {

	private static final String TAG = LbsActivity.class.getSimpleName();
	
	private static final String CONST_POS = "pos";
	private static final int CONST_MSG_SUCCESS = 0;
	private static final int CONST_TELPHY_SUCCESS = 1;
	private static final int CONST_MSG_FAIL = 1;
	private static final String CONST_TELPHY = "telphy";
	
	private TextView tv;
	private Handler handler;
	private LocationListener ll;
	private TextView telphyTv;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_lbs);
		tv = (TextView) findViewById(R.id.location_tv);
		telphyTv = (TextView) findViewById(R.id.telphy_tv);
		
		handler = new Handler() {
			@Override
			public void handleMessage(Message msg) {
				// TODO Auto-generated method stub
				super.handleMessage(msg);
				switch(msg.what) {
					case CONST_MSG_SUCCESS:
	
						Bundle bundle = msg.getData();
						String positonStr = bundle.getString(CONST_POS);
						tv.setText(positonStr);
						break;
					case CONST_TELPHY_SUCCESS:
						Bundle bundle1 = msg.getData();
						String positonStr1 = bundle1.getString(CONST_TELPHY);
						telphyTv.setText(positonStr1);
						break;
				}
			}
		};
		
		
		LocationManager lm = (LocationManager) getSystemService(LOCATION_SERVICE);
		
		Location location = lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
		
		updateLocation(location);
	
		
		ll = new LocationListener() {
			
			@Override
			public void onStatusChanged(String provider, int status, Bundle extras) {
				Log.i(TAG,"onStatusChanged[provider:"+provider+",status:"+status+"]");
				updateLocation(null);
			}
			
			@Override
			public void onProviderEnabled(String provider) {
				Log.i(TAG,"onStatusChanged[provider:"+provider+"]");
			}
			
			@Override
			public void onProviderDisabled(String provider) {
				Log.i(TAG,"onStatusChanged[provider:"+provider+"]");
			}
			
			@Override
			public void onLocationChanged(Location location) {
				Log.i(TAG,"onLocationChanged");
				updateLocation(location);
			}
		};
		
		
		lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 2000L, 0, ll);
		
		TelephonyManager tel = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
		CellLocation cellLocation = tel.getCellLocation(); 
		int nPhoneType = tel.getPhoneType(); 
		if (nPhoneType == 1 && cellLocation instanceof GsmCellLocation) { 
			this.isGsm = true; 
			GsmCellLocation gsmCellLocation = (GsmCellLocation) cellLocation; 
			int nGSMCID = gsmCellLocation.getCid();
			if (nGSMCID > 0) {
				if (nGSMCID != 65535) {
					this.cid = nGSMCID; 
					this.lac = gsmCellLocation.getLac();
				}
			}
		}
		
		String telphy = "cid:"+cid+",lac:"+lac;
		
		Bundle bundle = new Bundle();
		bundle.putString(CONST_TELPHY, telphy);
		
		Message msg = handler.obtainMessage(CONST_TELPHY_SUCCESS);
		msg.setData(bundle);
		msg.sendToTarget();
		
	}

	private void updateLocation(Location location) {
		if(location != null) {
			double latitude = location.getLatitude();
			double longtitude = location.getLongitude();
			
			String currPos = "latitude:"+latitude+",longtitude:"+longtitude;
			
			Bundle bundle = new Bundle();
			bundle.putString(CONST_POS, currPos);
			
			Message msg = handler.obtainMessage(CONST_MSG_SUCCESS);
			msg.setData(bundle);
			msg.sendToTarget();
		} else {
			String currPos = "No location found";
			
			Bundle bundle = new Bundle();
			bundle.putString(CONST_POS, currPos);
			
			Message msg = handler.obtainMessage(CONST_MSG_SUCCESS);
			msg.setData(bundle);
			msg.sendToTarget();
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.lbs, menu);
		return true;
	}
	


	private boolean isGsm;
	private int cid;
	private int lac;
}
